/postgis

getX3d from PostGIS Polyhedral Surface

Example on how to convert PostGIS geometry to X3D format

https://en.wikipedia.org/wiki/X3D

'POLYHEDRALSURFACE Z (
((12 0 10, 8 8 10, 8 10 20, 12 2 20, 12 0 10)),
((8 8 10, 0 12 10, 0 14 20, 8 10 20, 8 8 10)),
((0 12 10, -8 8 10, -8 10 20, 0 14 20, 0 12 10))
)'
), ST_GeomFromText(
'POLYHEDRALSURFACE Z (
((12 0 10, 8 8 10, 8 10 20, 12 2 20, 12 0 10)),
((8 8 10, 0 12 10, 0 14 20, 8 10 20, 8 8 10)),
((0 12 10, -8 8 10, -8 10 20, 0 14 20, 0 12 10))
)'
), ST_GeomFromEWKT('LINESTRING(-20 -20, -10 -10, 0 0, 10 10, 40 40)') );

create or replace function getX3d( variadic geoms geometry[] )
	returns text as 
	$$ 
	declare x3dPrefix text := '<X3D><Scene><Transform>';
	declare x3dSuffix text := '</Transform></Scene></X3D>';
	declare x3dShapePrefix text := '<Shape><Appearance><Material emissiveColor=''0 0 1''/></Appearance>';
	declare x3dShapeSuffix text := '</Shape>';
	
	declare x3dText text := x3dPrefix;
	declare x3dGeom geometry;
	declare x3dShape text = '';
	
	begin
	
	for idx in 1..array_length( geoms, 1 ) loop
		x3dGeom := geoms[idx];
		x3dShape := ST_AsX3D(x3dGeom);
		x3dText := x3dText || x3dShapePrefix || x3dShape || x3dShapeSuffix;
	end loop;
	
	x3dText := x3dText || x3dSuffix;
	
	return x3dText;
	end;
	$$
	language 'plpgsql';

-- SELECT '<X3D>
--   <Scene>
--     <Transform>
--       <Shape>
--        <Appearance>
--             <Material emissiveColor=''0 0 1''/>
--        </Appearance> ' ||
--        ST_AsX3D( ST_GeomFromText(
-- 'POLYHEDRALSURFACE Z (
-- ((12 0 10, 8 8 10, 8 10 20, 12 2 20, 12 0 10)),
-- ((8 8 10, 0 12 10, 0 14 20, 8 10 20, 8 8 10)),
-- ((0 12 10, -8 8 10, -8 10 20, 0 14 20, 0 12 10))
-- )'
-- ) ) || 
-- 	'</Shape> <Shape>
--        <Appearance>
--             <Material emissiveColor=''0 0 1''/>
--        </Appearance>' ||
 	   
-- 	'</Shape> <Shape>
--        <Appearance>
--             <Material emissiveColor=''0 0 1''/>
--        </Appearance>' ||
--        ST_AsX3D( ST_GeomFromEWKT('POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
-- ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
-- ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
-- ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )')) ||

-- 	ST_AsX3D( ST_GeomFromEWKT('LINESTRING(-20 -20, -10 -10, 0 0, 10 10, 40 40)')) ||

--       '</Shape>
--     </Transform>
--   </Scene>
-- </X3D>' As x3ddoc;

-- -- SELECT ST_GeomFromText(
-- -- 'POLYHEDRALSURFACE Z (
-- -- ((12 0 10, 8 8 10, 8 10 20, 12 2 20, 12 0 10)),
-- -- ((8 8 10, 0 12 10, 0 14 20, 8 10 20, 8 8 10)),
-- -- ((0 12 10, -8 8 10, -8 10 20, 0 14 20, 0 12 10))
-- -- )'
-- -- );

-- -- SELECT '<X3D>
-- --   <Scene>
-- --     <Transform>
-- --       <Shape>
-- --        <Appearance>
-- --             <Material emissiveColor=''0 0 1''/>
-- --        </Appearance> ' ||
-- --        ST_AsX3D( ST_GeomFromEWKT('POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
-- -- ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
-- -- ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
-- -- ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )')) ||
-- --       '</Shape>
-- --     </Transform>
-- --   </Scene>
-- -- </X3D>' As x3ddoc;



-- --    SELECT ST_GeomFromText(
-- -- 	   'POLYHEDRALSURFACEZ(
-- -- 	   ((1.0 1.0 0.0, 1.0 0.0 0.0, 0.0 0.0 0.0, 1.0 1.0 0.0)),
-- -- 	   ((1.0 0.0 0.0, 1.0 0.0 1.0, 0.0 0.0 0.0, 1.0 0.0 0.0)),
-- -- 	   ((1.0 0.0 0.0, 1.0 1.0 0.0, 1.0 1.0 1.0, 1.0 0.0 0.0)),
-- -- 	   ((1.0 0.0 1.0, 1.0 0.0 0.0, 1.0 1.0 1.0, 1.0 0.0 1.0)),
-- -- 	   ((1.0 1.0 1.0, 1.0 1.0 0.0, 0.0 1.0 1.0, 1.0 1.0 1.0)),
-- -- 	   ((1.0 1.0 1.0, 1.0 0.0 0.0, 0.0 1.0 0.0, 1.0 1.0 1.0)),
-- -- 	   ((1.0 1.0 0.0, 0.0 0.0 0.0, 0.0 1.0 1.0, 1.0 1.0 0.0)),
-- -- 	   ((1.0 0.0 0.0, 0.0 0.0 0.0, 0.0 1.0 0.0, 1.0 0.0 0.0)),
-- -- 	   ((0.0 0.0 0.0, 0.0 0.0 1.0, 0.0 1.0 1.0, 0.0 0.0 0.0)),
-- -- 	   ((0.0 0.0 0.0, 0.0 1.0 1.0, 0.0 1.0 0.0, 0.0 0.0 0.0)),
-- -- 	   ((1.0 1.0 0.0, 1.0 1.0 1.0, 1.0 0.0 0.0, 1.0 1.0 0.0)),
-- -- 	   ((1.0 0.0 0.0, 1.0 1.0 1.0, 1.0 0.0 1.0, 1.0 0.0 0.0)))',4328)

Subscribe to Lydon's blog

Get the latest posts delivered right to your inbox

Lydon

Lydon

Grow and grow and grow

Read More